package org.urchr.beorganized.dao.tables;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="users")
//@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class User implements Serializable{

	private static final long serialVersionUID = 8884123254415422409L;
	private int id;
	private String login;
	private String password;
	private String name;
	private String email;
	private boolean disabled = false;
	private Date createDate;
	private String dateFormat = "dd/MM/yy";
	private String timeFormat = "H:mm";
	private String language = "";
	private String timeZone = "";
	private int minuteOffset = 0;
	private Set<Session> sessions = new HashSet<Session>();
	private Set<Note> notes = new HashSet<Note>();
	private Set<Star> stars = new HashSet<Star>();
	private Set<Permission> permissions = new HashSet<Permission>();
	private int rootNoteID = 0;
	
	public void setId(int id) {
		this.id = id;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	@Column
	public String getLogin() {
		return login;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column
	public String getPassword() {
		return password;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column
	public String getName() {
		return name;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column
	public String getEmail() {
		return email;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	@Column
	public boolean isDisabled() {
		return disabled;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	@Column
	public String getDateFormat() {
		return dateFormat;
	}

	public void setTimeFormat(String timeFormat) {
		this.timeFormat = timeFormat;
	}

	@Column
	public String getTimeFormat() {
		return timeFormat;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	@Column
	public String getLanguage() {
		return language;
	}

	public void setSessions(Set<Session> sessions) {
		this.sessions = sessions;
	}

	@OneToMany(targetEntity=Session.class, mappedBy="user", cascade=CascadeType.ALL)
	public Set<Session> getSessions() {
		return sessions;
	}

	public void setNotes(Set<Note> notes) {
		this.notes = notes;
	}

	@OneToMany(targetEntity=Note.class, mappedBy="user", cascade=CascadeType.ALL)
	public Set<Note> getNotes() {
		return notes;
	}

	public void setStars(Set<Star> stars) {
		this.stars = stars;
	}

	@OneToMany(targetEntity=Star.class, mappedBy="user", cascade=CascadeType.ALL)
	public Set<Star> getStars() {
		return stars;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	@OneToMany(targetEntity=Permission.class, mappedBy="user", cascade=CascadeType.ALL)
	public Set<Permission> getPermissions() {
		return permissions;
	}
	
	@Override
	public String toString() {
		return "User id = "+getId()+", name = "+getName()+", login = "+getLogin();
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}

	@Column
	public String getTimeZone() {
		return timeZone;
	}

	public void setMinuteOffset(int minuteOffset) {
		this.minuteOffset = minuteOffset;
	}

	@Transient
	public int getMinuteOffsetByDate(Date date) {
		if(null==getTimeZone() || "".equals(getTimeZone())){
			return minuteOffset;			
		}
		TimeZone tz = TimeZone.getTimeZone(getTimeZone());
		return tz.getOffset(date.getTime())/1000/60;
	}
	
	@Column
	public int getMinuteOffset() {
		if(null==getTimeZone() || "".equals(getTimeZone())){
			return minuteOffset;			
		}
		TimeZone tz = TimeZone.getTimeZone(getTimeZone());
		return tz.getOffset(new Date().getTime())/1000/60;
	}

	public void setRootNoteID(int rootNoteID) {
		this.rootNoteID = rootNoteID;
	}

	@Transient
	public int getRootNoteID() {
		return rootNoteID;
	}

}
